package com.github.bobjoy.java.aop;


import com.github.bobjoy.java.aop.bean.Japanese;
import com.github.bobjoy.java.aop.bean.Person;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AopDemo {

    private static ApplicationContext ctx;

    /**
     * 1. 经典的基于代理（Spring的API）的AOP实现
     */
    @Test
    public void test1() {
        ctx = new ClassPathXmlApplicationContext("application-aop_1.xml");

        Person me = (Person) ctx.getBean("proxy");

        // 通过自动匹配，切面会自动匹配符合切入点的bean，会被自动代理，实现功能！
//        me = (Person) ctx.getBean("chinese");
        System.out.println(me.getClass());
        me.sleep();
    }

    /**
     * 2. 通过AspectJ提供的注解实现AOP
     */
    @Test
    public void test2() {
        ctx = new ClassPathXmlApplicationContext("application-aop_2.xml");
        Person me = (Person) ctx.getBean("chinese");
        System.out.println(me.getClass());
        me.sleep();
    }

    /**
     * 3. 使用Spring来定义纯粹的POJO切面
     */
    @Test
    public void test3() {
        ctx = new ClassPathXmlApplicationContext("application-aop_3.xml");
        Japanese japanese = ctx.getBean(Japanese.class);
        System.out.println(japanese.getClass());
        japanese.sleep();
    }

}
